package com.lry.web.config;

import com.lry.web.message.entity.Message;
import com.lry.web.message.service.MessageService;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Date;

@Configuration
public class RabbitMQConfig {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    MessageService messageService;

    //用于分辨是否成功
    /*
    0：初始化
    1：消息发送成功
    2：MQ接受失败
    3:消费者接受失败
     */
    public static Integer code = 0;

        @PostConstruct
        public void run(){
            rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
                /**
                 *
                 * @param message   消息对象
                 * @param replyCode 错误码
                 * @param replyText 错误信息
                 * @param exchange  交换机
                 * @param routingKey 路由键
                 */
                public void returnedMessage(org.springframework.amqp.core.Message message, int replyCode, String replyText, String exchange, String routingKey) {
                    RabbitMQConfig.code = 3;
                    messageService.setMessage(new Message("采集请求MQ接收失败",new Date()));

                }
            });
            rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
                /**
                 *
                 * @param correlationData 相关配置信息
                 * @param ack   exchange交换机 是否成功收到了消息。true 成功，false代表失败
                 * @param cause 失败原因
                 */
                public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                    if (ack) {
                        //接收成功
                        RabbitMQConfig.code = 1;
                    } else {
                        //接收失败
                        RabbitMQConfig.code = 2;
                        messageService.setMessage(new Message("采集请求MQ接收失败",new Date()));

                    }
                }
            });

        }


    //交换机名
    public static final String GATHEREXCHANGE_NAME = "gather_exchange";
    public static final String SURVIVEEXCHANGE_NAME = "survive_exchange";
    public static final String UPDATEEXCHANGE_NAME = "update_exchange";

    //队列名
    public static final String FICTIONQUEUE_NAME = "fiction_queue";
    public static final String CARTOONQUEUE_NAME = "cartoon_queue";
    public static final String  VIDEOQUEUE_NAME = "video_queue";
    public static final String SURVIVEQUEUE_NAME = "survive_queue";
    public static final String FUPDATEEQUEUE_NAME = "Fupdate_queue";
    public static final String CUPDATEEQUEUE_NAME = "Cupdate_queue";
    public static final String VUPDATEEQUEUE_NAME = "Vupdate_queue";


    //fiction交换机
    @Bean("gatherExchange")
    public Exchange fictionExchange(){
        return ExchangeBuilder.topicExchange(GATHEREXCHANGE_NAME).durable(true).build();
    }

    //survive交换机
    @Bean("surviveExchange")
    public Exchange surviveExchange(){
        return ExchangeBuilder.topicExchange(SURVIVEEXCHANGE_NAME).durable(true).build(); }

    //update交换机
    @Bean("updateExchange")
    public Exchange updateExchange(){
        return ExchangeBuilder.topicExchange(UPDATEEXCHANGE_NAME).durable(true).build(); }

    //fiction队列
    @Bean("fictionQueue")
    public Queue fictionQueue(){
        return QueueBuilder.durable(FICTIONQUEUE_NAME).build();
    }

    //cartoon队列
    @Bean("cartoonQueue")
    public Queue cartoonQueue(){
        return QueueBuilder.durable(CARTOONQUEUE_NAME).build();
    }

    //fiction队列
    @Bean("videoQueue")
    public Queue videoQueue(){
        return QueueBuilder.durable(VIDEOQUEUE_NAME).build();
    }

    //survive队列
    @Bean("surviveQueue")
    public Queue surviveQueue(){
        return QueueBuilder.durable(SURVIVEQUEUE_NAME).build();
    }
    //Fupdate队列
    @Bean("FupdateQueue")
    public Queue FupdateFueue(){
        return QueueBuilder.durable(FUPDATEEQUEUE_NAME).build();
    }
    //Cupdate队列
    @Bean("CupdateQueue")
    public Queue CupdateQueue(){
        return QueueBuilder.durable(CUPDATEEQUEUE_NAME).build();
    }
    //Vupdate队列
    @Bean("VupdateQueue")
    public Queue VupdateQueue(){
        return QueueBuilder.durable(VUPDATEEQUEUE_NAME).build();
    }

    //队列和交互机绑定关系
    @Bean
    public Binding fictionQueueExchange(@Qualifier("fictionQueue") Queue queue, @Qualifier("gatherExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("fiction.#").noargs();
    }
    @Bean
    public Binding cartoonQueueExchange(@Qualifier("cartoonQueue") Queue queue, @Qualifier("gatherExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("cartoon.#").noargs();
    }

    @Bean
    public Binding videoQueueExchange(@Qualifier("videoQueue") Queue queue, @Qualifier("gatherExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("video.#").noargs();
    }


    @Bean
    public Binding surviveQueueExchange(@Qualifier("surviveQueue") Queue queue, @Qualifier("surviveExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("#").noargs();
    }
    @Bean
    public Binding updateFQueueExchange(@Qualifier("FupdateQueue") Queue queue, @Qualifier("updateExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("fiction.#").noargs();
    }
    @Bean
    public Binding updateCQueueExchange(@Qualifier("CupdateQueue") Queue queue, @Qualifier("updateExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("cartoon.#").noargs();
    }
    @Bean
    public Binding updateVQueueExchange(@Qualifier("VupdateQueue") Queue queue, @Qualifier("updateExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("video.#").noargs();
    }

}